package com.bootdo.member.service.impl;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.mall.service.MallOrderService;
import com.bootdo.member.commons.MemberIntegralsUtils;
import com.bootdo.member.controller.PageUtilsByLu;
import com.bootdo.member.domain.ShequMember;
import com.bootdo.member.domain.ShequMemberStatistics;
import com.bootdo.member.service.ShequMemberService;
import com.bootdo.system.domain.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.member.dao.ShequIntegralRecordDao;
import com.bootdo.member.domain.ShequIntegralRecord;
import com.bootdo.member.service.ShequIntegralRecordService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ShequIntegralRecordServiceImpl implements ShequIntegralRecordService {
	@Autowired
	private ShequIntegralRecordDao shequIntegralRecordDao;
	@Autowired
	private ShequMemberService shequMemberService;
	@Autowired
	private ShequIntegralRecordService shequIntegralRecordService;
	@Autowired
	private MallOrderService mallOrderService;

	@Override
	public ShequIntegralRecord get(Long id){
		return shequIntegralRecordDao.get(id);
	}
	
	@Override
	public List<ShequIntegralRecord> list(Map<String, Object> map){
		return shequIntegralRecordDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return shequIntegralRecordDao.count(map);
	}
	
	@Override
	public int save(ShequIntegralRecord shequIntegralRecord){
	
		if(shequIntegralRecord.getId() != null) {
			return shequIntegralRecordDao.update(shequIntegralRecord);
		}else {
			return shequIntegralRecordDao.save(shequIntegralRecord);
		}		
	
		
	}
	
	@Override
	public int update(ShequIntegralRecord shequIntegralRecord){
		return shequIntegralRecordDao.update(shequIntegralRecord);
	}
	
	@Override
	public int remove(Long id){
		return shequIntegralRecordDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return shequIntegralRecordDao.batchRemove(ids);
	}

	@Override
	public int updateState(int state, Long id) {
		return shequIntegralRecordDao.updateState(state,id);

	}

	@Override
	public List<Map> tongJi(HashMap<String, Object> map) {
		return shequIntegralRecordDao.tongJi(map);
	}

	@Override
	@Transactional
	public R updateIntegrals(String mobile, int type, ShequIntegralRecord shequIntegralRecord) {
		if(shequIntegralRecord!=null){
			if(type!=1){

				shequIntegralRecord.setState(2);//已经取消  叫做 拒绝
				/* shequIntegralRecord.setCountType(2);//1为 增加  0为消减  2为没有扣除或者增加*/
				shequIntegralRecordService.save(shequIntegralRecord);

                /*ShequIntegralRecord shequIntegralRecord = new ShequIntegralRecord();
                shequIntegralRecord.setIntegrals(shequIntegralRecords.getIntegrals());
                shequIntegralRecord.setMemberId(shequIntegralRecords.getMemberId());
                shequIntegralRecord.setMemberName(shequIntegralRecords.getMemberName());
                shequIntegralRecord.setMemberMobile(shequIntegralRecords.getMemberMobile());
                shequIntegralRecord.setRemarks("时间为："+ DateUtils.format(shequIntegralRecords.getCreateTime(),"yyyy年MM月dd日 HH时mm分ss秒")+"的订单取消，积分加回");
                shequIntegralRecord.setState(1);//已确认
                shequIntegralRecord.setCountType(1);//1为 增加  0为消减  2为没有扣除或者增加
                shequIntegralRecord.setOperationType(0);//该操作为其他人员
                shequIntegralRecord.setCommunityId(shequIntegralRecords.getCommunityId());
                shequIntegralRecord.setCommunityName(shequIntegralRecords.getCommunityName());
                shequIntegralRecordService.save(shequIntegralRecord);*/
				return R.ok("你已取消");

			}
			shequIntegralRecord.setState(1);//已经确认
			MemberIntegralsUtils memberIntegralsUtils = new MemberIntegralsUtils();
			shequIntegralRecord.setCountType(0); boolean b = memberIntegralsUtils.updateIntegrals(shequIntegralRecord.getIntegrals(), mobile, 2, shequMemberService);
			if(!b){
				return R.ok("兑换失败，积分不足");
			}
			int save = shequIntegralRecordService.save(shequIntegralRecord);
			if(save<0){
				throw new RuntimeException("积分更新失败");
			}
			return R.ok("兑换成功");
		}else{
			return R.error("该条记录有误！请刷新重试");
		}
	}

	@Override
	public PageUtilsByLu list2(Map<String, Object> params) {
		Long communityId=null;//定义一个 小区id  如果小区id为前台传过来的  则可以获取到 否则为空
		String endTime=null;
		String startTime=null;
		String memberName=null;
		String memberMobile=null;
		String communityName=null;
		String limit=null;
		String offset=null;
		try {
			String communityid= params.get("communityId").toString();
			communityId=Long.parseLong(communityid);
		}catch (Exception e){
			e.printStackTrace();
		}
		try {
			endTime = (String)params.get("endTime");
			startTime = (String)params.get("startTime");
			memberName = (String) params.get("memberName");
			memberMobile = (String)params.get("memberMobile");
			communityName = (String)params.get("communityName");
			limit = (String)params.get("limit");
			offset = (String)params.get("offset");
		}catch (Exception e){
			e.printStackTrace();
		}

		UserDO user = ShiroUtils.getUser();
		if(user.getUserType()!=1){
			communityId = user.getCommunityId();
		}
		HashMap<String, Object> map = new HashMap<>();
		map.put("endTime",endTime);
		map.put("startTime",startTime);
		map.put("realName",memberName);
		map.put("mobile",memberMobile);
		map.put("communityName",communityName);
		map.put("communityId",communityId);
		try {
			map.put("limit",Integer.parseInt(limit));
			map.put("offset",Integer.parseInt(limit)*Integer.parseInt(offset));
		}catch (Exception e){
			e.printStackTrace();
		}

		List<ShequMember> list = shequMemberService.listDescByMoney(map);//查询出所有的社区下的用户
		int total = shequMemberService.count(map);
		ArrayList<ShequMemberStatistics> shequMemberStatistics = new ArrayList<>();
		for( ShequMember shequMember:list){
			ShequMemberStatistics statistics = new ShequMemberStatistics();
			/*map.put("memberId",shequMember.getId());*/
			/**
			 * 初始化参数列表
			 */
			map.remove("memberMobile");
			map.remove("countType");
			map.remove("operationType");
			map.remove("countType");
			map.remove("type");
			map.remove("payStatusBy");
			map.remove("payStatus");

			map.put("memberMobile",shequMember.getMobile());
			map.put("countType","0");
			List<Map> conInt = shequIntegralRecordService.tongJi(map);//获取到消费积分的总数

			map.put("operationType","1");
			List<Map> conIntPro = shequIntegralRecordService.tongJi(map);//获取到物业消费积分的总数

			map.remove("operationType");
			map.put("countType","1");
			List<Map> addInt = shequIntegralRecordService.tongJi(map);//获取到增加积分的总数

			map.put("type","1");//type 为微信支付
			map.put("payStatusBy","0");//除了为0 的所有 payStatus 状态
			List<Map> orderAll = mallOrderService.tongJi(map);//微信支付金额统计 退款金额

			map.put("type","2");//type 为微信支付
			map.remove("payStatusBy");//除了为0 的所有 payStatus 状态
			map.put("payStatus","5");//除了为0 的所有 payStatus 状态
			List<Map> TkOrderIntAll = mallOrderService.tongJi(map);// 退款积分统计

			map.put("type","2");
			map.remove("payStatus");
			map.put("payStatusBy","0");//除了为0 的所有 payStatus 状态
			List<Map> jiFenOrderAll = mallOrderService.tongJi(map);//积分支付下但数量统计
			String conintegral="0";
			String addintegral="0";
			String totalMoney2="0";
			String tkMoney="0";
			String totalProInt="0";
			String wxTotalNum="0";
			String jiFenTotalNum="0";
			String totalProNum="0";
			String tkTotalInt="0";
			if(orderAll.size()!=0&&orderAll!=null){
				totalMoney2 = orderAll.get(0).get("totalMoney").toString();
				tkMoney = orderAll.get(0).get("tkMoney").toString();
				wxTotalNum = orderAll.get(0).get("orderNumber").toString();
			}
			if(TkOrderIntAll.size()!=0&&TkOrderIntAll!=null){
				tkTotalInt = TkOrderIntAll.get(0).get("tkMoney").toString();
			}
			if(jiFenOrderAll.size()!=0&&jiFenOrderAll!=null){
				jiFenTotalNum = jiFenOrderAll.get(0).get("orderNumber").toString();
			}
			if(conInt.size()!=0&&conInt!=null){
				conintegral = conInt.get(0).get("conintegral").toString();
			}
			if(conIntPro.size()!=0&&conIntPro!=null){
				totalProInt = conIntPro.get(0).get("conintegral").toString();
				totalProNum = conIntPro.get(0).get("numbers").toString();
			}
			if(addInt.size()!=0&&addInt!=null){
				addintegral = addInt.get(0).get("conintegral").toString();
			}

			statistics.setMemberId(shequMember.getId());//会员id
			statistics.setMemberName(shequMember.getRealName());//会员名称
			statistics.setMemberMobile(shequMember.getMobile());//会员电话
			statistics.setConTotalIntegral(Double.parseDouble(conintegral));//消费总积分
			statistics.setAddTotalIntegral(Double.parseDouble(addintegral));//增加总积分
			statistics.setConTotalMoney(Double.parseDouble(totalMoney2));//消费总金额
			statistics.setTkMoney(Double.parseDouble(tkMoney));//退款总金额
			statistics.setOrderNumber(Integer.parseInt(wxTotalNum)+Integer.parseInt(jiFenTotalNum)+Integer.parseInt(totalProNum));//下单数
			statistics.setWxOrderNumber(Integer.parseInt(wxTotalNum));//下单数
			statistics.setJifenOrderNumber(Integer.parseInt(jiFenTotalNum));//下单数
			statistics.setPropertyExchangeTotalInt(Double.parseDouble(totalProInt));//物业消费积分
			statistics.setPropertyExchangeTotalNumber(Integer.parseInt(totalProNum));//物业兑换次数
			statistics.setTkTotalInt(Double.parseDouble(tkTotalInt));//退换总积分
			statistics.setShopConInt(Double.parseDouble(conintegral)-Double.parseDouble(totalProInt));//购物消费积分

			shequMemberStatistics.add(statistics);
		}

		Double addTotalIntegralAll=0.00;
		Double conTotalIntegralAll=0.00;
		Double conTotalMoneyAll=0.00;
		Integer orderNumberAll=0;
		Double tkMoneyAll=0.00;
		Double tkTotalInt=0.00;
		for (ShequMemberStatistics statistics:shequMemberStatistics){
			addTotalIntegralAll=statistics.getAddTotalIntegral()+addTotalIntegralAll;
			conTotalIntegralAll=statistics.getConTotalIntegral()+conTotalIntegralAll;
			conTotalMoneyAll=statistics.getConTotalMoney()+conTotalMoneyAll;
			orderNumberAll=statistics.getOrderNumber()+orderNumberAll;
			tkMoneyAll=statistics.getTkMoney()+tkMoneyAll;
			tkTotalInt=statistics.getTkTotalInt()+tkTotalInt;
		}
		/* Collections.sort(shequMemberStatistics);*/
		PageUtilsByLu pageUtils = new PageUtilsByLu(shequMemberStatistics, total,addTotalIntegralAll,conTotalIntegralAll,conTotalMoneyAll,orderNumberAll,tkMoneyAll,tkTotalInt);
		return pageUtils;
	}

}
